/***
 * Copyright (c) 2021-2031 murenchao
 * fig is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.software.fig.reflect

class ReflectUtils {
    def static getReflectFields(Class<?> zlass) {
        zlass.declaredFields
    }

    def static setReflectFields(Class<?> zlass, String name, Object instance, Object value) {
        var field = zlass.getDeclaredField(name);
        field.accessible = true;
        field.set(instance, value);
    }

    def static getMethod(Class<?> zlass, String name, Class<?> ... parameterTypes) {
        var method = zlass.getMethod(name, parameterTypes);
        method.accessible = true;
        return method;
    }

    def static getReflectField(Class<?> zlass, String name) {
        var field = zlass.getDeclaredField(name);
        field.accessible = true;
        return field;
    }

    def static newInstance(Class<?> zlass, Object ... args) {
        return newInstance(zlass, null, args);
    }

    def static newInstance(Class<?> zlass, Class<?>[] parameterTypes, Object ... args) {
        if (parameterTypes === null) {
            return zlass.getConstructor().newInstance();
        } else {
            return zlass.getConstructor(parameterTypes).newInstance(args);
        }
    }
}
